home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / kiss-0.11 / kiss-0 / kiss / src / runcmd.c < prev    next >
C/C++ Source or Header  |  1995-03-23  |  2KB  |  89 lines

  1. #include "kiss.h"
  2.  
  3. /* define DEBUG_ONE_CMD to get one shot at gdb'ing the child process */
  4. /* then make a -g version of the command to see and set a trace in the code */
  5.  
  6. /*
  7. #define DEBUG_ONE_CMD
  8. */
  9.  
  10. void runcmd (Stringstack s)
  11. {
  12.     register int
  13.     pid,
  14.     background = 0;
  15.     Stringstack
  16.     tmp1,
  17.     tmp2,
  18.     tmp3;
  19.  
  20. #   ifdef DEBUG_ONE_CMD
  21.     runchild (s);
  22. #   endif
  23.  
  24.     if (! s.nstr)
  25.     return;
  26.  
  27.     dumpstack ("parser result:", s);
  28.     
  29.     /* cmd goes into history */
  30.     addtohistory (s);
  31.  
  32.     /* expand s to whatever it may mean, unless it's an alias definition  */
  33.     if (strcmp (s.str [0], "alias"))
  34.     {
  35.     tmp1 = expandalias (s);        /* expand aliases */
  36.     dumpstack ("after alias expansion:" , tmp1);
  37.     
  38.     tmp2 = reexpand (tmp1);        /* expand wildcards and vars */
  39.     clearstack (&tmp1);
  40.     dumpstack ("after wordexp expansion:", tmp2);
  41.     
  42.     expandtilde (tmp2);        /* expand ~/ to homedir */
  43.     dumpstack ("after ~/ expansion:" , tmp2);
  44.  
  45.     tmp3 = expandbackquotes (tmp2);
  46.     clearstack (&tmp2);
  47.     dumpstack ("after `bla` expansion:", tmp3);
  48.     }
  49.     else
  50.     tmp3 = copystringstack (s, 0, s.nstr - 1);
  51.  
  52.     /* reset getopt variables */
  53.     opterr =  optopt = optind = 0;
  54.     optarg = NULL;
  55.  
  56.     /* reset last status */
  57.     laststatus = 0;
  58.  
  59.     dumpstack ("running part:", tmp3);
  60.  
  61.     if (! runinternal (tmp3))            /* try to run as internal */
  62.     {                        /* otherwise: via fork */
  63.     if (! strcmp (tmp3.str [tmp3.nstr - 1], "&"))
  64.     {
  65.         background++;
  66.         free (tmp3.str [tmp3.nstr - 1]);
  67.         tmp3.nstr--;
  68.     }
  69.  
  70.     if ( (pid = fork ()) == -1 )        /* try forking */
  71.     {
  72.         warning ("fork failure");
  73.         return;
  74.     }
  75.     else if (! pid)                /* child: run that cmd */
  76.     {
  77.         setshlvl ();
  78.         runchild (tmp3, background);
  79.         exit (0);
  80.     }
  81.     else                    /* parent: wait for child */
  82.         waitforchild (tmp3.str [0], pid, background);
  83.     }
  84.     
  85.     if (! flags.supressstat && laststatus && ! background)
  86.     printf ("[%s: exited with %d]\n", tmp3.str [0], laststatus);
  87.     clearstack (&tmp3);
  88. }
  89.